﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using MINERP.InfoConfig.Controller;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;
using MINERP.COM;

namespace MINERP.InfoConfig
{
    public partial class FrmAreaManager : FrmMDIChildFormTemplate
    {
        private AreaController      _controller = new AreaController();
        GridSubArea                 _area = null;
        static FrmAreaManager instance = null;
        private FrmAreaManager()
        {
            InitializeComponent();
        }

        public static FrmAreaManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmAreaManager();
                }
                return instance;
            }
        }

        private void FrmAreaManager_Load(object sender, EventArgs e)
        {
            _controller.GridControl = gridControlArea;
            _controller.View = gridViewArea;
            _controller.GetDate();

            //_controller.ComoBox = cmbSubAreaType;
            //_controller.LinkAreaType();
        }

        private void gridViewArea_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _area = (GridSubArea)gridViewArea.GetFocusedRow();
            if (_area != null)
            {
                TxtAlarmTime.Text = _area.OutTimeAlarmTime.ToString();
                txtAreaName.Text = _area.SubAreaName;
                txtdelayTime.Text = _area.GridClearTime.ToString();
                txtHeadCharName.Text = _area.nameHeadChar;
                txtXGridNum.Text = _area.widthNum.ToString();
                txtYGridNum.Text = _area.heightNum.ToString();
                chkIsUseful.Checked = _area.isUseful;
                switch (_area.Type)
                {
                    case 0:
                        cmbSubAreaType.Text = "照片分堆分区";
                        break;
                    case 1:
                        cmbSubAreaType.Text = "委外商分堆分区";
                        break;
                    case 2:
                        cmbSubAreaType.Text = "商户分堆分区";
                        break;
                    default:
                        break;
                }
            }

        }
        //添加分区
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtAreaName.Focus();
                return;
            }
            if (CheckData())
            {
                GridSubArea area = new GridSubArea();
                area.OutTimeAlarmTime =  Convert.ToDouble(TxtAlarmTime.Text);
                area.SubAreaName =    txtAreaName.Text;
                area.GridClearTime =   Convert.ToDouble(txtdelayTime.Text);   
                area.nameHeadChar = txtHeadCharName.Text;
                area.widthNum =Convert.ToInt32(txtXGridNum.Text); 
                area.heightNum =Convert.ToInt32(txtYGridNum.Text);
                area.isUseful = chkIsUseful.Checked;
                switch (cmbSubAreaType.Text)
                {
                    case "照片分堆分区":
                        area.Type = Convert.ToInt32(SubAreaType.Photo);
                        break;
                    case "委外商分堆分区":
                        area.Type = Convert.ToInt32(SubAreaType.OutShop);
                        break;
                    case "商户分堆分区":
                        area.Type = Convert.ToInt32(SubAreaType.Shop);
                        break;
                    case "车间合册分区": area.Type = Convert.ToInt32(SubAreaType.FactorySubOrder);
                        break;
                    case "内部返单分并分区": area.Type = Convert.ToInt32(SubAreaType.InnerReturn);
                        break;

                }
                _controller.Add(area);

                //link datagrid
                _controller.GridControl = gridControlArea;
                _controller.View = gridViewArea;
                _controller.GetDate();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //更新分区
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                TxtAlarmTime.Focus();
                return;
            }
            if (CheckData())
            {
                GridSubArea area = _controller.GetAreaObj(_area.SubAreaID);
                area.OutTimeAlarmTime = Convert.ToDouble(TxtAlarmTime.Text);
                area.SubAreaName = txtAreaName.Text;
                area.GridClearTime = Convert.ToDouble(txtdelayTime.Text);
                area.nameHeadChar = txtHeadCharName.Text;
                area.widthNum = Convert.ToInt32(txtXGridNum.Text);
                area.heightNum = Convert.ToInt32(txtYGridNum.Text);
                area.isUseful = chkIsUseful.Checked;
                switch (cmbSubAreaType.Text)
                {
                    case "照片分堆分区":
                        area.Type = Convert.ToInt32(SubAreaType.Photo);
                        break;
                    case "委外商分堆分区":
                        area.Type = Convert.ToInt32(SubAreaType.OutShop);
                        break;
                    case "商户分堆分区":
                        area.Type = Convert.ToInt32(SubAreaType.Shop);
                        break;
                    case "车间合册分区": area.Type = Convert.ToInt32(SubAreaType.FactorySubOrder);
                        break;
                    case "内部返单分并分区": area.Type = Convert.ToInt32(SubAreaType.InnerReturn);
                        break;

                }
                _controller.Update(area);


                //refresh datagrid
                _controller.GridControl = gridControlArea;
                _controller.View = gridViewArea;
                _controller.GetDate();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        // 删除分区
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                GridSubArea area = _controller.GetAreaObj(_area.SubAreaID);
                _controller.Delete(area);

                //refresh datagrid
                _controller.GridControl = gridControlArea;
                _controller.View = gridViewArea;
                _controller.GetDate();
            }
        }
        // 取消分区
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtAreaName.Enabled = false;
            TxtAlarmTime.Enabled = false;
            txtdelayTime.Enabled = false;
            txtYGridNum.Enabled = false;
            txtXGridNum.Enabled = false;
            txtHeadCharName.Enabled = false;
            cmbSubAreaType.Enabled = false;
            chkIsUseful.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }
        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtAreaName.Text == string.Empty)
            {
                txtAreaName.Focus();
                MessageBox.Show("分区名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (TxtAlarmTime.Text == string.Empty)
            {
                TxtAlarmTime.Focus();
                MessageBox.Show("报警时间不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtdelayTime.Text == string.Empty)
            {
                txtdelayTime.Focus();
                MessageBox.Show("格子延迟清空时间不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtYGridNum.Text == string.Empty)
            {
                txtYGridNum.Focus();
                MessageBox.Show("纵向格子数不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtXGridNum.Text == string.Empty)
            {
                txtXGridNum.Focus();
                MessageBox.Show("横向格子数不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtHeadCharName.Text == string.Empty)
            {
                txtHeadCharName.Focus();
                MessageBox.Show("开头字不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;

            }
            if (cmbSubAreaType.Text == string.Empty)
            {
                cmbSubAreaType.Focus();
                MessageBox.Show("分区类型不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {

            txtAreaName.Enabled = bl;
            TxtAlarmTime.Enabled = bl;
            txtdelayTime.Enabled = bl;
            txtYGridNum.Enabled = bl;
            txtXGridNum.Enabled = bl;
            txtHeadCharName.Enabled = bl;
            cmbSubAreaType.Enabled = bl;
            chkIsUseful.Enabled = bl;

            txtAreaName.Text = string.Empty;
            TxtAlarmTime.Text = string.Empty;
            txtdelayTime.Text = string.Empty;
            txtYGridNum.Text = string.Empty;
            txtXGridNum.Text = string.Empty;
            txtHeadCharName.Text = string.Empty;
            cmbSubAreaType.Text = string.Empty;
            chkIsUseful.Text = string.Empty;

            if (bl)
            {
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {

            txtAreaName.Enabled = bl;
            TxtAlarmTime.Enabled = bl;
            txtdelayTime.Enabled = bl;
            txtYGridNum.Enabled = bl;
            txtXGridNum.Enabled = bl;
            txtHeadCharName.Enabled = bl;
            cmbSubAreaType.Enabled = bl;
            chkIsUseful.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }
        private void FrmAreaManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void txtYGridNum_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (txtYGridNum.Text.Trim() != string.Empty)
                {
                    Convert.ToInt32(txtYGridNum.Text.Trim());
                }
            }
            catch
            {
                MessageBox.Show("请输入整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtYGridNum.Text = string.Empty;
            }
        }

        private void txtXGridNum_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                Convert.ToInt32(txtXGridNum.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtXGridNum.Text = string.Empty;
            }
        }

        private void TxtAlarmTime_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (TxtAlarmTime.Text.Trim() != string.Empty)
                {
                    Convert.ToDouble(TxtAlarmTime.Text.Trim());
                }
            }
            catch
            {
                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                TxtAlarmTime.Text = string.Empty;
            }
        }

        private void txtdelayTime_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (txtdelayTime.Text.Trim() != string.Empty)
                {
                    Convert.ToDouble(txtdelayTime.Text.Trim());
                }
            }
            catch
            {

                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtdelayTime.Text = string.Empty;
            }
        }
       
    }
}
